package com.bsj.power.common.util;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.bsj.tools.DateUntil;
import org.apache.commons.lang3.StringUtils;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/4/16.
 */
public class DateUtils {

    public final static String TIME_STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public final static String TIME_STANDARD_FORMAT2 = "yyyyMMddHHmmss";

    public final static String TIME_STANDARD_FORMAT_YMD = "yyyyMMdd";

    public final static String TIME_STANDARD_FORMAT_YMDH = "yyyyMMddHH";
    public final static String TIME_STANDARD_FORMAT_YMDHM = "yyyyMMddHHmm";
    public final static String TIME_STANDARD_FORMAT_YMDHM2 = "yyMMddHHmm";
    public final static String TIME_STANDARD_FORMAT_HM = "HHmm";
    public final static String TIME_STANDARD_FORMAT_HMS = "HH:mm:ss";

    public final static String CURRENCY = "yyyy-MM-dd HH:mm";

    public final static String yearMonthDay = "yyyy-MM-dd";

    private final static SimpleDateFormat sdf = new SimpleDateFormat(TIME_STANDARD_FORMAT);

    private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern(TIME_STANDARD_FORMAT);

    /**
     * 转化格式
     *
     * @return
     */
    public static String dateToStr(String format, Date date) {
        if (StringUtils.isEmpty(format) || date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
        return sdf.format(date);

    }

    /**
     * localDateTime时间格式化
     * @param format 格式内容
     * @param date 待格式化时间
     * @return
     */
    public static String localDateTimeToStr(String format, LocalDateTime date) {
        if (date == null) {
            return null;
        }
        String str = null;
        if (format == null) {
            str = date.format(formatter);
        } else {
            DateTimeFormatter mat = DateTimeFormatter.ofPattern(format);
            str = date.format(mat);
        }
        return str;

    }

    /**
     * 获得时间转化类
     *
     * @param str 转化的字符串
     * @return
     */
    public static SimpleDateFormat getDateFormat(String str) {
        if (StringUtils.isEmpty(str)) {
            return sdf;
        }
        return new SimpleDateFormat(str);
    }

    /**
     * 获得时间转化类
     *
     * @return
     */
    public static SimpleDateFormat getDateFormat() {
        return getDateFormat(null);
    }

    /**
     * 字符串转化为date
     *
     * @param format
     * @param date
     * @return
     * @throws Exception
     */
    public static Date strToDate(String format, String date) {
        if (StringUtils.isEmpty(format) || date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static Date hh(String format, String date) throws ParseException {
        if (StringUtils.isEmpty(format) || date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }


    /**
     * 字符串转化为date
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static Date strToDate(String date) {
        return strToDate(TIME_STANDARD_FORMAT, date);
    }

    /**
     * 方法重载
     *
     * @param date
     * @return
     */
    public static String dateToStr(Date date) {
        return dateToStr(TIME_STANDARD_FORMAT, date);
    }

    /**
     * 时间转换
     *
     * @param time
     * @return
     */
    public static String timeToStr(Time time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_STANDARD_FORMAT_HMS);
        return simpleDateFormat.format(time);
    }

    /**
     * 方法重载
     *
     * @param date
     * @return
     */
    public static String dateToStr2(Date date) {
        return dateToStr(TIME_STANDARD_FORMAT2, date);
    }

    public static String dateToYmdStr(Date date) {
        return dateToStr(TIME_STANDARD_FORMAT_YMD, date);
    }

    public static String dateToYmdhStr(Date date) {
        return dateToStr(TIME_STANDARD_FORMAT_YMDH, date);
    }

    public static List<String> dateToYmdhStrList(long time, int num) {
        List<String> ymdhList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            String ymdh = dateToStr(TIME_STANDARD_FORMAT_YMDH, new Date(time - 1000L * 60 * 60 * i));
            ymdhList.add(ymdh);
        }
        return ymdhList;
    }

    /**
     * 把秒数转为页面需要的 天，小时，
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static String separatedFormatByTime(Date startTime, Date endTime) {
        if (startTime == null || endTime == null) {
            return "";
        }
        return separatedFormatByTime(startTime.getTime(), endTime.getTime());
    }

    /**
     * 把秒数转为页面需要的 天，小时，
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static String separatedFormatByTime(long startTime, long endTime) {
        String result = "";
        long diff = endTime - startTime;
        if (startTime == 0) {
            //开始时间为零，则默认10分钟
            diff = 10 * 60 * 1000;
        }
        diff = diff / 1000;
        long day = 0;
        long hour = 0;
        long min = 0;
        day = diff / (24 * 60 * 60);
        hour = (diff / (60 * 60) - day * 24);
        min = ((diff / 60) - day * 24 * 60 - hour * 60);
        if (day > 0) {
            result = day + "天" + hour + "小时" + min + "分";
        } else if (hour > 0) {
            result = hour + "小时" + min + "分";
        } else if (min > 0) {
            result = min + "分";
        }
        return result;
    }

    /**
     * 计算时间
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long calculationTime(Date startTime, Date endTime) {
        if (startTime == null || endTime == null) {
            return 0;
        }
        return (endTime.getTime() - startTime.getTime());
    }

    /**
     * 计算间隔时间返回多少秒
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static int calculationTimeBySecond(Date startTime, Date endTime) {
        return (int) (calculationTime(startTime, endTime) / 1000);
    }

    /**
     * 把秒数转为页面需要的 天，小时，
     *
     * @param time
     * @return
     */
    public static String separatedFormatByTime(long time) {
        String result = "";
        if (time == 0) {
            return result;
        }
        long diff = time / 1000;
        long day = 0;
        long hour = 0;
        long min = 0;
        day = diff / (24 * 60 * 60);
        hour = (diff / (60 * 60) - day * 24);
        min = ((diff / 60) - day * 24 * 60 - hour * 60);
        if (day > 0) {
            result = day + "天" + hour + "小时" + min + "分";
        } else if (hour > 0) {
            if (min == 0) {
                result = hour + "小时";
            } else {
                result = hour + "小时" + min + "分";
            }
        } else if (min > 0) {
            result = min + "分";
        }
        return result;
    }

    /**
     * 精确到秒
     *
     * @param time
     * @return
     */
    public static String separatedFormatByTime2Sec(long time) {
        String result = "";
        if (time == 0) {
            return result;
        }
        long diff = time / 1000;
        long day = 0;
        long hour = 0;
        long min = 0;
        day = diff / (24 * 60 * 60);
        hour = (diff / (60 * 60) - day * 24);
        min = ((diff / 60) - day * 24 * 60 - hour * 60);
        if (day > 0) {
            result = day + "天" + hour + "小时" + min + "分";
        } else if (hour > 0) {
            result = hour + "小时" + min + "分";
        } else if (min > 0) {
            result = min + "分";
        }
        return result;
    }

    /**
     * 精确到秒
     *
     * @param oldTime
     * @param newTime
     * @return
     */
    public static String getDistanceTimeL(long oldTime, long newTime) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff;
        if (oldTime < newTime) {
            diff = newTime - oldTime;
        } else {
            diff = oldTime - newTime;
        }
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String ret = "";
        if (day != 0) {
            return day + "天" + hour + "小时" + min + "分" + sec + "秒";
        }
        if (hour != 0) {
            return hour + "小时" + min + "分" + sec + "秒";
        }
        if (min != 0) {
            return min + "分" + sec + "秒";
        }
        return sec + "秒";
    }

    /**
     * 查询几天后的日期
     *
     * @param date
     * @param days
     * @return
     */
    public static Date dateByDays(Date date, int days) {
        // 加上新加的日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 不是直接用月，而是用日*31
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * 查询几年后的日期
     *
     * @param date
     * @param years
     * @return
     */
    public static Date dateByYears(Date date, int years) {
        // 加上新加的日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 不是直接用月，而是用日*31
        calendar.add(Calendar.YEAR, years);
        return calendar.getTime();
    }

    /**
     * 得到几天前的集合
     *
     * @param date
     * @param day
     * @return
     */
    public static List<String> getDateAfter(Date date, int day) {
        List<String> list = new LinkedList<>();
        for (int i = 0; i < day; i++) {
            Date days = dateByDays(date, -i);
            list.add(dateToStr("yyyyMMdd", days));
        }
        return list;
    }

    public static List<String> getDateAfter(Date date, int day, String str) {
        List<String> list = new LinkedList<>();
        for (int i = 0; i < day; i++) {
            Date days = dateByDays(date, -i);
            list.add(dateToStr(str, days));
        }
        return list;
    }

    /**
     * 2个时间相隔多少天
     *
     * @param beginTime
     * @param endTime
     * @return 相隔天数的集合
     */
    public static List<String> getDateAfter(Date beginTime, Date endTime) {
        int between_days = (int) ((endTime.getTime() - beginTime.getTime()) / (1000 * 3600 * 24));
        return getDateAfter(endTime, between_days + 1, "yyyy-MM-dd");
    }

    /**
     * 2个时间相隔多少天
     *
     * @param begin
     * @param end
     * @return 相隔天数的集合
     */
    public static List<String> getDateAfter(String begin, String end) {
        Date beginTime = strToDate("yyyy-MM-dd", begin);
        Date endTime = strToDate("yyyy-MM-dd", end);
        return getDateAfter(beginTime, endTime);
    }

    /**
     * 添加指定月份方法
     *
     * @param date
     * @param month
     * @return
     */
    public static Date addMonthTime(Date date, int month) {
        if (date == null || month == 0) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 不是直接用月，而是用日*31
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }


    /**
     * 获得下个小时的间隔时间
     *
     * @param date
     * @return
     */
    private static long getHourNextInterval(Date date) {
        if (date == null) {
            return 0;
        }
        String str = DateUtils.dateToStr("mm:ss", date);
        String[] arr = str.split(":");
        int min = 59 - Integer.parseInt(arr[0]);
        int second = 60 - Integer.parseInt(arr[1]);
        return (min * 60 + second);
    }

    /**
     * 根据日期和间隔算出中间相隔的时间段(比较特殊的算法)
     *
     * @param date
     * @param time
     * @return
     */
    public static Map<String, Long> getDateStrList(Date date, long time) {
        Map<String, Long> map = new HashMap<>();
        if (date == null || time == 0) {
            return map;
        }
        //获得小时
        String str = DateUtils.dateToStr("HH", date);
        int strInt = Integer.parseInt(str);
        long hourInterval = getHourNextInterval(date);
        if (hourInterval >= time) {
            map.put(strInt + "", time);
        } else {
            map.put(strInt + "", hourInterval);
            long timeInterval = time - hourInterval;
            int except = (int) (timeInterval / (60 * 60));
            int remainder = (int) (timeInterval % (60 * 60));
            for (int i = 1; i < except + 1; i++) {
                String key = dateHourOperation(strInt, i) + "";
                if (map.containsKey(key)) {
                    long value = map.get(key);
                    value = value + (60 * 60);
                    map.put(key, value);
                } else {
                    map.put(key, (60 * 60L));
                }
            }
            String lastKey = dateHourOperation(strInt, except + 1) + "";
            map.put(lastKey, (long) remainder);
        }
        return map;
    }

    /**
     * 小时加减运算
     *
     * @param strInt
     * @param interval
     * @return
     */
    private static int dateHourOperation(int strInt, int interval) {
        int i = strInt + interval;
        if (i < 24) {
            return i;
        } else {
            int except = i / 24;
            return (i - 24 * except);
        }
    }

    /**
     * 时间显示格式
     *
     * @param ctime
     * @return
     */
    public static String showTime(Date ctime) {
        String r = "";
        if (ctime == null) {
            return r;
        }
        long nowtimelong = System.currentTimeMillis();
        long ctimelong = ctime.getTime();
        long result = Math.abs(nowtimelong - ctimelong);
        if (result < 60000) {// 一分钟内
            long seconds = result / 1000;
            if (seconds == 0) {
                r = "刚刚";
            } else {
                r = seconds + "秒前";
            }
        } else if (result >= 60000 && result < 3600000) {
            // 一小时内
            long seconds = result / 60000;
            r = seconds + "分钟前";
        } else if (result >= 3600000 && result < 86400000) {
            // 一天内
            long seconds = result / 3600000;
            r = seconds + "小时前";
        } else if (result >= 86400000 && result < 1702967296) {
            // 三十天内
            long seconds = result / 86400000;
            r = seconds + "天前";
        } else {// 日期格式
            r = dateToStr("MM-dd", ctime);
        }
        return r;
    }

    /**
     * 电动车显示日期方法
     *
     * @param startDate
     * @return
     */
    public static String showTimeByECar(Date startDate) {
        String result = "";
        //获取当地时区
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (startDate.getTime() + offSet) / 86400000;
        int interval = (int) (start - today);
        String dateStr = dateToStr("HH:mm:ss", startDate);
        if (interval == -2) {
            result = "前天 " + dateStr;
        } else if (interval == -1) {
            result = "昨天 " + dateStr;
        } else if (interval == 0) {
            result = "今天 " + dateStr;
        } else {
            result = dateToStr(startDate);
        }
        return result;
    }


    /**
     * 电动车显示日期方法
     *
     * @param ctime
     * @return
     */
    public static String showTimeByECar(String ctime) {
        String r = "";
        if (StringUtils.isEmpty(ctime)) {
            return r;
        }
        Date ctimeDate = strToDate(ctime);
        return showTimeByECar(ctimeDate);
    }


    /**
     * 时间显示格式
     *
     * @param ctime
     * @return
     */
    public static String showTime(String ctime) {

        String r = "";
        if (ctime == null) {
            return r;
        }
        Date ctimeDate = strToDate(ctime);
        return showTime(ctimeDate);
    }

    /**
     * 现在离凌晨还有多长时间(秒)
     *
     * @return
     */
    public static long toMorningTime() {
        LocalTime midnight = LocalTime.MIDNIGHT;
        LocalDate today = LocalDate.now();
        LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
        LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);
        return TimeUnit.NANOSECONDS.toSeconds(Duration.between(LocalDateTime.now(), tomorrowMidnight).toNanos());
    }


    public static List<String> getYearMonthDayHour(long sTime, long eTime) {
        // 判断时间大小
        if (sTime >= eTime) {
            return null;
        }
        List<String> retList = new LinkedList<String>();
        // 从开始时间，不停地加一天，直到时间大于eTime为止
        Calendar endCal = Calendar.getInstance();
        endCal.setTimeInMillis(eTime);
        // 开始的天，它每循环一次加一天
        long calDay = 0;
        // 结束的那一天
        long endDay = 0;
        // 对集合编号
        int HOUR_OF_DAYEND = endCal.get(Calendar.HOUR_OF_DAY);
        // 每天的时间段编号
        int dayEndHourNum = HOUR_OF_DAYEND;

        endDay = endCal.get(Calendar.YEAR) * 1000000 + (endCal.get(Calendar.MONTH) + 1) * 10000
                + endCal.get(Calendar.DAY_OF_MONTH) * 100 + dayEndHourNum;
        // 中间计算的时间
        long calTime = sTime;
        do {
            // 计算的时间
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(calTime);
            // 对集合编号
            int HOUR_OF_DAY = cal.get(Calendar.HOUR_OF_DAY);

            // 每天的时间段编号
            int dayHourNum = HOUR_OF_DAY;

            // 获取集合
            String strTime = String.format("%04d%02d%02d%02d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1,
                    cal.get(Calendar.DAY_OF_MONTH), dayHourNum);

            // 把获取的集合添加到返回的list中
            retList.add(strTime);

            // 加1小时
            cal.add(Calendar.HOUR_OF_DAY, 1);
            calTime = cal.getTimeInMillis();

            // 对集合编号
            int HOUR_OF_DAYNEW = cal.get(Calendar.HOUR_OF_DAY);
            // 每天的时间段编号
            int dayHourNumNew = HOUR_OF_DAYNEW;
            // 重新计算时间段数值
            calDay = cal.get(Calendar.YEAR) * 1000000 + (cal.get(Calendar.MONTH) + 1) * 10000
                    + cal.get(Calendar.DAY_OF_MONTH) * 100 + dayHourNumNew;
        } while (calDay <= endDay);
        return retList;
    }

    /**
     * 获取距离23点59分59秒的毫秒数
     *
     * @param time 时间戳
     * @return long
     * @author 杨小华
     * @date 2018/5/10 15:32
     */
    public static long getLongTimeByTwelve(long time) {
        //今天零点零分零秒的毫秒数
        long zero = time / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
        //今天23点59分59秒的毫秒数
        long twelve = zero + 24 * 60 * 60 * 1000 - 1;
        return twelve - time;
    }

    public static String getTimeDifferenceSingle(long diff) {
        StringBuffer sb = new StringBuffer();
        long nd = 1000 * 24 * 60 * 60; // 一天的毫秒数
        long nh = 1000 * 60 * 60; // 一小时的毫秒数
        long nm = 1000 * 60; // 一分钟的毫秒数
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        } else {
            sb.append(min + "分");
        }
        return sb.toString();
    }

    /**
     * 将毫秒值转为正常时间间隔，精确到秒
     *
     * @param diff
     * @return java.lang.String
     * @author 杨小华
     * @date 2018/5/17 16:49
     */
    public static String getTimeDiffSingle(long diff) {
        StringBuffer sb = new StringBuffer();
        long nd = 1000 * 24 * 60 * 60; // 一天的毫秒数
        long nh = 1000 * 60 * 60; // 一小时的毫秒数
        long nm = 1000 * 60; // 一分钟的毫秒数
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        //无线设备追车组 +1秒
        long sec = diff % nd % nh % nm / 1000L + 1;
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        }
        if (min > 0) {
            sb.append(min + "分");
        }
        if (sec > 0) {
            sb.append(sec + "秒");
        }
        return sb.toString();
    }

    /**
     * 判断小于等于设置的时间
     *
     * @param time  需要比较的时间戳
     * @param hours 小时
     * @return boolean
     * @author 杨小华
     * @date 2018/6/28 15:44
     */
    public static boolean isDiffByHours(long time, int hours) {
        long systemTime = System.currentTimeMillis();

        // 系统时间减终端时间的差值，单位毫秒
        long diff = systemTime - time;
        if (diff <= hours * 60 * 60 * 1000L) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断webSocket是否在线
     *
     * @param time 数据接收时间的毫秒值
     * @return true 在线，false离线
     */
    public static boolean isOnLine(long time, int min) {
        long systemTime = System.currentTimeMillis();

        // 系统时间减终端时间的差值，单位毫秒
        long diff = systemTime - time;
        if (diff <= min * 60 * 1000L) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断两个时间是否是同一天
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_STANDARD_FORMAT_YMD);
        return sdf.format(date1).equals(sdf.format(date2));
    }



    public static int nowDateInt() {
        String dd = DateUtils.dateToStr("dd", new Date());
        int i = Integer.parseInt(dd);
        return i;
    }

    //计算某个时间段6个月内的时间
    public static List<String> getMinusMonth(int minusMonth) {
        List<String> list = new LinkedList<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM");
        LocalDate now = LocalDate.now();
        for (int i = 0; i < minusMonth; i++) {
            LocalDate months = now.minusMonths(i);
            list.add(df.format(months));
        }
        return list;
    }


    /**
     * 转成6字节的bcd时间 yyyymmdd hhmmss
     *
     * @param date
     * @return
     */
    public static byte[] timeSec(Date date) {
        byte[] byt = new byte[6];
        // 日期 6位
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        byt[0] = (byte) Integer.parseInt(year + "", 16);
        int month = cal.get(Calendar.MONTH) + 1;
        byt[1] = (byte) Integer.parseInt(month + "", 16);
        int day = cal.get(Calendar.DATE);
        byt[2] = (byte) Integer.parseInt(day + "", 16);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        byt[3] = (byte) Integer.parseInt(hour + "", 16);
        int minute = cal.get(Calendar.MINUTE);
        byt[4] = (byte) Integer.parseInt(minute + "", 16);
        int second = cal.get(Calendar.SECOND);
        byt[5] = (byte) Integer.parseInt(second + "", 16);
        return byt;

    }

    /**
     * 4字节的bcd  转成 utc时间
     *
     * @param bcdTime
     * @param offset
     * @param len
     * @return
     */
    public static long bcdToDatatime_4(byte[] bcdTime, int offset, int len) {
        if (offset + 4 <= len) {

            // 日期
            int year = 1970;
            int month = 0;
            int date = 1;

            // 年 // 22
            year = (((bcdTime[offset + 1] & 0xF0) >> 4) * 10 + (bcdTime[offset + 1] & 0x0F) * 1)
                    + (((bcdTime[offset] & 0xF0) >> 4) * 10 + (bcdTime[offset] & 0x0F) * 1) * 100;

            // 月 Calendar月份用0-11表示，也就是说0表示1月，11表示12月 // 23
            month = (((bcdTime[offset + 2] & 0xF0) >> 4) * 10 + (bcdTime[offset + 2] & 0x0F) * 1);

            // 日 // 24
            date = ((bcdTime[offset + 3] & 0xF0) >> 4) * 10 + (bcdTime[offset + 3] & 0x0F) * 1;

            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month - 1, date, 0, 0, 0);

            return calendar.getTimeInMillis();
        }
        return 0;
    }


    /**
     * UTC时间转换成6字节的BCD时间YY-MM-DD-HH-MM-SS
     * <p>
     * 毫秒
     *
     * @param time
     * @return
     */
    public static byte[] utcToBCDTime(long time) {
        byte[] bcdTime = new byte[6];
        Calendar begD = Calendar.getInstance();
        begD.setTimeInMillis(time);
        int offset = 0;
        // 需要后两位2018，需要18
        int year = begD.get(Calendar.YEAR) - 2000;
        int month = begD.get(Calendar.MONTH) + 1;
        int day = begD.get(Calendar.DAY_OF_MONTH);
        int hour = begD.get(Calendar.HOUR_OF_DAY);
        int minute = begD.get(Calendar.MINUTE);
        int second = begD.get(Calendar.SECOND);
        // year
        bcdTime[offset++] = Conv.dec2bcd((byte) year);
        // month
        bcdTime[offset++] = Conv.dec2bcd((byte) month);
        // day
        bcdTime[offset++] = Conv.dec2bcd((byte) day);
        // hour
        bcdTime[offset++] = Conv.dec2bcd((byte) hour);
        // minute
        bcdTime[offset++] = Conv.dec2bcd((byte) minute);
        // second
        bcdTime[offset++] = Conv.dec2bcd((byte) second);
        return bcdTime;
    }


    /**
     * 6字节的BCD时间转换成utc时间
     *
     * @param bcdTime
     * @return
     */
    public static long bcdToLongtime(byte[] bcdTime, int offset) {
        // 日期
        int year = 1970;
        int month = 0;
        int date = 1;
        int hour = 0;
        int minute = 0;
        int second = 0;

        // 年
        year = (((bcdTime[offset] & 0xF0) >> 4) * 10 + (bcdTime[offset] & 0x0F) * 1) + 2000;

        // 月 Calendar月份用0-11表示，也就是说0表示1月，11表示12月
        month = (((bcdTime[offset + 1] & 0xF0) >> 4) * 10 + (bcdTime[offset + 1] & 0x0F) * 1);

        // 日
        date = ((bcdTime[offset + 2] & 0xF0) >> 4) * 10 + (bcdTime[offset + 2] & 0x0F) * 1;


        // 时
        hour = ((bcdTime[offset + 3] & 0xF0) >> 4) * 10 + (bcdTime[offset + 3] & 0x0F) * 1;
        minute = ((bcdTime[offset + 4] & 0xF0) >> 4) * 10 + (bcdTime[offset + 4] & 0x0F) * 1;

        // 秒
        second = ((bcdTime[offset + 5] & 0xF0) >> 4) * 10 + (bcdTime[offset + 5] & 0x0F) * 1;

        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date, hour, minute, second);

        return calendar.getTimeInMillis();

    }

    /**
     * 根据差值返回多长之间前或多长时间后
     */
    public static Long getDistanceTimeMin(long time1, long time2) {
        long min = 0;

        if (time1 < time2) {
            min = (time2 - time1) / 1000 / 60;
        } else {
            min = (time1 - time2) / 1000 / 60;
        }

        return min;
    }

    /**
     * 当前天数往前推多少天
     *
     * @param dayNum
     * @return
     */
    public static Set<String> getDayKey(int dayNum) {
        Set<String> dayKeySet = new HashSet<>();
        long nowTime = System.currentTimeMillis();
        for (int i = 0; i < dayNum; i++) {
            Date date = new Date(nowTime - 1000 * 60 * 60 * 24 * i);
            String ymd = DateUtils.dateToYmdStr(date);
            dayKeySet.add(ymd);
        }
        return dayKeySet;
    }

    /**
     * 比较时间的大小
     *
     * @param doString
     * @param activeString
     * @return
     */
    public static boolean compareDate(String doString, String activeString) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yearMonthDay);
        Date doTime = simpleDateFormat.parse(doString);
        Date activeTime = simpleDateFormat.parse(activeString);

        if (activeTime.getTime() < doTime.getTime()) {
            return true;
        }
        return false;
    }

    /**
     * 根据年月日获取String 时间数据
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static String getDateByYearMothDay(Integer year, Integer month, Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_MONTH, month - 1);
        calendar.set(Calendar.HOUR_OF_DAY, day);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return dateToStr(calendar.getTime());
    }

    /**
     * 获取当前时间几天前的时间(零点)
     *
     * @param day
     * @return long
     */
    public static long getSpecifyTimeBeforeTime(int day) {
        //获取一个 calendar日历对象, 该对象可以进行时间的计算
        Calendar calendar = Calendar.getInstance();
        //指定天数之前的时间
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime().getTime();
    }

    /**
     * 计算多少秒|分|时|天前
     *
     * @param timeDifference 时间差
     * @return java.lang.String
     */
    public static String format(long timeDifference) {
        String pubDateStr = "刚刚";
        //分钟
        if (timeDifference / 60 < 60) {
            pubDateStr = timeDifference + "分钟前";
        } else if (timeDifference / 60 / 60 < 24) {
            //小时
            pubDateStr = (timeDifference / 60 / 60) + "小时前";
        } else if (timeDifference / 60 / 60 / 24 != 0) {
            //天
            pubDateStr = (timeDifference / 60 / 60 / 24) + "天前";
        }
        return pubDateStr;
    }

    /**
     * 时间计算
     *
     * @param date  指定时间
     * @param value 负为指定时间之前的时间，正为指定时间之后的时间
     * @param type  1：年 2：月 3：日 4：时 5：分 6：秒 7：毫秒 8：周
     * @return long
     */
    public static Date timeCalculation(Date date, int value, int type) {
        Calendar dateTime = Calendar.getInstance();
        dateTime.setTime(date);
        switch (type) {
            case 1:
                dateTime.add(Calendar.YEAR, value);
                break;
            case 2:
                dateTime.add(Calendar.MONTH, value);
                break;
            case 3:
                dateTime.add(Calendar.DAY_OF_MONTH, value);
                break;
            case 4:
                dateTime.add(Calendar.HOUR_OF_DAY, value);
                break;
            case 5:
                dateTime.add(Calendar.MINUTE, value);
                break;
            case 6:
                dateTime.add(Calendar.SECOND, value);
                break;
            case 7:
                dateTime.add(Calendar.MILLISECOND, value);
                break;
            case 8:
                dateTime.add(Calendar.DAY_OF_WEEK_IN_MONTH, value);
                break;
        }
        return dateTime.getTime();
    }


    /**
     * 获取某天最小的时间（Java8的LocalDateTime）
     *
     * @param date
     * @return
     */
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取某天最大的时间（Java8的LocalDateTime）
     *
     * @param date
     * @return
     */
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 使用日历对象获取昨天最小的时间，返回string类型
     *
     * @return
     */
    public static String getStartOfYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return sdf.format(calendar.getTime());
    }

    /**
     * 使用日历对象获取昨天最大的时间，返回string类型
     *
     * @return
     */
    public static String getEndOfYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取本周开始（最小）时间
     *
     * @return
     */
    public static String getStartOfThisWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取本周结束（最大）时间
     *
     * @return
     */
    public static String getEndOfThisWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取本月开始时间（最小）
     *
     * @return
     */
    public static String getStartOfThisMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取本月结束时间（最大）
     *
     * @return
     */
    public static String getEndOfThisMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
        return sdf.format(calendar.getTime());
    }




    /**
     * 使用日历对象获取当天最小的时间，返回string类型
     *
     * @param date
     * @return
     */
    public static String getStartOfDay2(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new SimpleDateFormat(TIME_STANDARD_FORMAT).format(calendar.getTime());
    }

    /**
     * 使用日历对象获取当天最大的时间，返回string类型
     *
     * @param date
     * @return
     */
    public static String getEndOfDay2(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new SimpleDateFormat(TIME_STANDARD_FORMAT).format(calendar.getTime());
    }

    /**
     * 获取开始时间
     *
     * @param dateStr
     * @return
     */
    public static Date getStartTime(String dateStr) {
        Date data = DateUntil.strToDate(DateUntil.CURRENCY_DAY, dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(data);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取结束时间
     *
     * @param dateStr
     * @return
     */
    public static Date getEndTime(String dateStr) {
        Date data = DateUntil.strToDate(DateUntil.CURRENCY_DAY, dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(data);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取往前12个月份的最前一个月份
     *
     * @param date
     * @param year
     * @return java.lang.String
     */
    public static String getDateFromYearsAgo(Date date, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR) - year, calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
        return dateToStr(calendar.getTime());
    }

    /**
     * 获取规格化时间
     *
     * @param date
     * @return
     */
    public static String getNormalizationDate(String date) {
        return DateUtil.format(DateUntil.strToDate(DateUntil.TIME_STANDARD_FORMAT, date), DateUntil.TIME_STANDARD_FORMAT);
    }

    /**
     * 获取某月的月初时间
     *
     * @param month 根据当前时间往前推的月数
     * @return
     */
    public static String getCertainMonthTime(int month) {
        //获取一个 calendar日历对象, 该对象可以进行时间的计算
        Calendar calendar = Calendar.getInstance();
        //指定天数之前的时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - month);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.TIME_STANDARD_FORMAT);
        return sdf.format(calendar.getTime());
    }

    /**
     * 换算下车时间
     *
     * @param Ontime  yyyy-MM-dd HH:mm:ss
     * @param offTime HH:mm:ss
     * @return
     */
    public static String convertOffTime(String Ontime, String offTime) throws ParseException {
        String[] split = offTime.split(":");
        String[] strings = Ontime.split(" ")[1].split(":");
        SimpleDateFormat sf = new SimpleDateFormat(DateUtils.TIME_STANDARD_FORMAT);
        Calendar calendar = Calendar.getInstance();
        if (Integer.valueOf(split[0]) < Integer.valueOf(strings[0])) {
            Date timeNow = sf.parse(Ontime);
            calendar.setTime(timeNow);
            System.out.println("当前日期:" + sf.format(timeNow));
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(split[0]));
            calendar.set(Calendar.MINUTE, Integer.valueOf(split[1]));
            calendar.set(Calendar.SECOND, Integer.valueOf(split[2]));
            calendar.set(Calendar.MILLISECOND, 0);
            System.out.println("增加后日期:" + sf.format(calendar.getTime()));
        } else {
            Date timeNow = sf.parse(Ontime);
            calendar.setTime(timeNow);
            calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(split[0]));
            calendar.set(Calendar.MINUTE, Integer.valueOf(split[1]));
            calendar.set(Calendar.SECOND, Integer.valueOf(split[2]));
            calendar.set(Calendar.MILLISECOND, 0);
            System.out.println("增加后日期:" + sf.format(calendar.getTime()));
        }
        return sf.format(calendar.getTime());
    }

    /**
     * 时间转换成6字节bcd
     *
     * @return
     */
    public static byte[] time2bcd6(String time) {
        byte[] data = new byte[6];
        if (StringUtils.isBlank(time)) {
            return data;
        }
        Date date = strToDate(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        String newDate = sdf.format(date);
        DataUtil.str2Bcd(data, 0, newDate);
        return data;
    }

    /**
     * 两个日期之间相差的天数计算 以日为单位
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        //不同年
        if (year1 != year2) {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                //闰年
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                    timeDistance += 366;
                }
                //不是闰年
                else {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else {// 同年
            return day2 - day1;
        }
    }

    /**
     * 获取两个日期相差的月数
     */
    public static int getMonthDiff(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
        return monthsDiff;
    }

    /**
     * 获取两个日期的月份差
     * 实际上包含了开始日期与结束日期的月份
     *
     * @param startTime
     * @param endTime
     * @time 2024/5/14 10:06
     */
    public static int getMonthsDifference(String startTime, String endTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.yearMonthDay);
        LocalDate startDate = LocalDate.parse(startTime, formatter);
        LocalDate endDate = LocalDate.parse(endTime, formatter);
        int years = endDate.getYear() - startDate.getYear();
        int months = (endDate.getMonthValue() - startDate.getMonthValue()) + 1;
//        if (endDate.getDayOfMonth() < startDate.getDayOfMonth()) {
//            months--;
//        }
        return years * 12 + months;
    }

    /**
     * 秒转时分秒
     *
     * @param seconds
     * @return
     */
    public static String secToTime(int seconds) {
        int hour = seconds / 3600;
        int minute = (seconds - hour * 3600) / 60;
        int second = (seconds - hour * 3600 - minute * 60);

        StringBuilder sb = new StringBuilder();
        if (hour > 0) {
            sb.append(hour).append("小时");
        }
        if (minute > 0) {
            sb.append(minute).append("分");
        }
        if (second > 0) {
            sb.append(second).append("秒");
        }
        return sb.toString();
    }


    /**
     * 计算两个时间间隔（单位：小时）
     * @param start 开始时间
     * @param end 结束时间
     * @return
     */
    public static Double minusDateToHour(Date start, Date end) {
        LocalDateTime timeStart = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime timeEnd = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        // 计算两个时间之间的差值，结果以分钟为单位
        long hoursBetween = ChronoUnit.MINUTES.between(timeStart, timeEnd);
        return NumberUtil.div(hoursBetween, 60, 2);
    }

    /**
     * 计算时间间隔 (单位：分钟)
     * @param start
     * @param end
     * @return
     */
    public static Long minusDateToMinutes(Date start, Date end) {
        LocalDateTime timeStart = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime timeEnd = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        // 计算两个时间之间的差值，结果以分钟为单位
        long hoursBetween = ChronoUnit.MINUTES.between(timeStart, timeEnd);
        return hoursBetween;
    }

    /**
     * 获取N天前的0点时间
     * @param now 当前时间
     * @param n 多少天前
     * @return Date
     */
    public static Date nDaysAgo(LocalDateTime now, int n) {
        //获取多少天前的时间
        LocalDateTime minus = now.minus(n, ChronoUnit.DAYS);
        //该时间的0点时间
        LocalDateTime time = minus.with(LocalTime.MIN);
        //将LocalDateTime 转换成Date
        Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
        return date;
    }

    /**
     * 根据日期计算多少岁（日期与当前时间隔了多少年）
     *

     * @param dateStr 出生日期
     * @return java.lang.Integer
     * @time 2024/7/29 14:26
     */
    public static Integer getAgeByDate(String dateStr) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(TIME_STANDARD_FORMAT);
        LocalDate ageLocalDate = LocalDate.parse(dateStr, df);
        LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        Period period = Period.between(ageLocalDate, localDate);
        return period.getYears();
    }

    /**
     * 判断当前时间是否在每小时0分，10分，20分，30分，40分，50分对应的那分钟内
     * @param now
     * @return
     */
    public static Boolean checkTimeByMinute(LocalDateTime now) {
        // 开始时间
        LocalDateTime s1Time = now.withMinute(0).withSecond(0).withNano(0);
        LocalDateTime s2Time = now.withMinute(10).withSecond(0).withNano(0);
        LocalDateTime s3Time = now.withMinute(20).withSecond(0).withNano(0);
        LocalDateTime s4Time = now.withMinute(30).withSecond(0).withNano(0);
        LocalDateTime s5Time = now.withMinute(40).withSecond(0).withNano(0);
        LocalDateTime s6Time = now.withMinute(50).withSecond(0).withNano(0);
        // 结束时间
        LocalDateTime e1Time = now.withMinute(0).withSecond(59).withNano(0);
        LocalDateTime e2Time = now.withMinute(10).withSecond(59).withNano(0);
        LocalDateTime e3Time = now.withMinute(20).withSecond(59).withNano(0);
        LocalDateTime e4Time = now.withMinute(30).withSecond(59).withNano(0);
        LocalDateTime e5Time = now.withMinute(40).withSecond(59).withNano(0);
        LocalDateTime e6Time = now.withMinute(50).withSecond(59).withNano(0);
        boolean isInRangeS1 = !now.isBefore(s1Time) && !now.isAfter(e1Time);
        boolean isInRangeS2 = !now.isBefore(s2Time) && !now.isAfter(e2Time);
        boolean isInRangeS3 = !now.isBefore(s3Time) && !now.isAfter(e3Time);
        boolean isInRangeS4 = !now.isBefore(s4Time) && !now.isAfter(e4Time);
        boolean isInRangeS5 = !now.isBefore(s5Time) && !now.isAfter(e5Time);
        boolean isInRangeS6 = !now.isBefore(s6Time) && !now.isAfter(e6Time);
        if (isInRangeS1 || isInRangeS2 || isInRangeS3
                || isInRangeS4 || isInRangeS5 || isInRangeS6) {
            return true;
        }
        return false;
    }

    /**
     * 根据时间范围返回对应的包序号,每个10分钟的最后一分钟用于清除之前的数据
     * @param now
     * @return
     */
    public static Integer checkTimeByMinuteInt(LocalDateTime now) {
        // 开始时间
        LocalDateTime s1Time = now.withMinute(1).withSecond(0).withNano(0);
        LocalDateTime s2Time = now.withMinute(11).withSecond(0).withNano(0);
        LocalDateTime s3Time = now.withMinute(21).withSecond(0).withNano(0);
        LocalDateTime s4Time = now.withMinute(31).withSecond(0).withNano(0);
        LocalDateTime s5Time = now.withMinute(41).withSecond(0).withNano(0);
        LocalDateTime s6Time = now.withMinute(51).withSecond(0).withNano(0);
        // 结束时间
        LocalDateTime e1Time = now.withMinute(9).withSecond(0).withNano(0);
        LocalDateTime e2Time = now.withMinute(19).withSecond(0).withNano(0);
        LocalDateTime e3Time = now.withMinute(29).withSecond(0).withNano(0);
        LocalDateTime e4Time = now.withMinute(39).withSecond(0).withNano(0);
        LocalDateTime e5Time = now.withMinute(49).withSecond(0).withNano(0);
        LocalDateTime e6Time = now.withMinute(59).withSecond(0).withNano(0);
        boolean isInRangeS1 = !now.isBefore(s1Time) && !now.isAfter(e1Time);
        boolean isInRangeS2 = !now.isBefore(s2Time) && !now.isAfter(e2Time);
        boolean isInRangeS3 = !now.isBefore(s3Time) && !now.isAfter(e3Time);
        boolean isInRangeS4 = !now.isBefore(s4Time) && !now.isAfter(e4Time);
        boolean isInRangeS5 = !now.isBefore(s5Time) && !now.isAfter(e5Time);
        boolean isInRangeS6 = !now.isBefore(s6Time) && !now.isAfter(e6Time);
        if (isInRangeS1) {
            return 0;
        } else if (isInRangeS2) {
            return 1;
        } else if (isInRangeS3) {
            return 2;
        } else if (isInRangeS4) {
            return 3;
        } else if (isInRangeS5) {
            return 4;
        } else if (isInRangeS6) {
            return 5;
        }
        return 6;
    }

}